<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/4
 * Time: 14:04
 */

namespace App\Http\Admin\Administration\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use App\Http\Admin\Administration\Models\NoticePolicy;
use Framework\Extend\Helpers\Upload;
class NoticePolicyController extends Controller
{
    /**
     * 获取新闻政策列表
     * @Author sxy
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     */
    public function noticePolicyList()
    {
        if (request()->isMethod('post')) {
            $oa_company_id=session('current_operation_company_id',1);
            $page = request('pageCurrent');
            $pageSize = request('pageSize');
            $search = request()->except(['pageCurrent', 'pageSize']);
            $searchField = [];
            $user_role=request()->user('admin')->roles;
            if($user_role[0]['name']=='admin'){
                $oa_company_ids=[0,$oa_company_id];
            }else{
                $oa_company_ids=[$oa_company_id];
            }
            if (!empty($search)) {
                foreach ($search as $k => $v) {
                    if ($v || $v == "0") $searchField[] = [$k, 'like', '%' . $v . '%'];
                }
            }

            $noticePolicy = new NoticePolicy();
            return $noticePolicy->getNoticePolicyList($page, $pageSize, $searchField,$oa_company_ids);
        } else {
            return $this->view('notice-policy-list');
        }
    }

    /**
     * 新增新闻政策
     * @Author sxy
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function noticePolicyAdd(){

        /**
         * todo
         * 可能需要新闻发布人id
         */
        try {
            if (request()->isMethod('post')) {
                $request = request()->all();
                $oa_company_id=session('current_operation_company_id',1);
                $request['oa_company_id'] = $oa_company_id;
                $user_role=request()->user('admin')->roles;
                if($user_role[0]['name']=='admin'){
                    $request['oa_company_id']=0;
                }
                $rules = [
                    'title' => 'required',
                    'content' => 'required',
                    'main_image_url' => 'required',
                    'type' => 'required',
                ];
                $this->validateRequestParameters($request, $rules, true);
                $noticePolicy = new NoticePolicy();
                $re = $noticePolicy->addNoticePolicy($request);
                if (!$re) return $this->ajaxFail();
                return $this->ajaxSuccess();
            } else {
                return $this->view('notice-policy-add');
            }
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }


    /**
     * 上传新闻政策图片
     * @Author sxy
     * @return array
     */
    public function noticePolicyImgUpload()
    {
        $img = request()->file('file');
        $fileInfo = Upload::uploadFile($img, 'notice-policy-img');
        $imageSize = getimagesize($fileInfo['fullPath']);

        $thumbnailWidth = round($imageSize[0] / 4);
        $thumbnailHeight = round($imageSize[1] / 4);

        $thumbnailPath = getThumbnailPath($fileInfo['fullPath']);

        Upload::imageThumbnail($fileInfo['fullPath'], $thumbnailPath, $thumbnailWidth, $thumbnailHeight, 20);

//        $imgRatio = filesize($img) / 1024 / 30;
//        $imagePathArr = explode('/', $fileInfo['fullPath']);
//        $endSub = count($imagePathArr) - 1;
//        $imagePathArr[$endSub] = 'WeChat/' . $imagePathArr[$endSub];
//        $shareImgUrl = implode('/', $imagePathArr);dd($shareImgUrl);
//        $img = Image::make($shareImgUrl)->resize($thumbnailWidth, $thumbnailHeight);
//        $img->save($thumbnailPath, ceil(100 / $imgRatio));
        \DebugBar::disable();
        return [
            'statusCode' => 200,
            'filename' => '/' . $fileInfo['fullPath']
        ];
    }

    /**
     * 编辑新闻政策
     * @Author sxy
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function noticePolicyEdit()
    {
        try {
            if (request()->isMethod('post')) {

                $request = request()->all();
                $rules = [
                    'title' => 'required',
                    'content' => 'required',
                    'type' => 'required|numeric',

                ];
                $this->validateRequestParameters($request, $rules, true);
                $noticePolicy = new NoticePolicy();
                $re = $noticePolicy->editNoticePolicy($request);
                if (!$re) return $this->ajaxFail();
                return $this->ajaxSuccess();
            } else {
                $request = request()->all();
                $id = $request['id'];
                $noticePolicy = new NoticePolicy();
                $noticeInfo = $noticePolicy->getNoticePolicyInfo($id);
                return $this->view('notice-policy-edit')->with('noticeInfo', $noticeInfo);
            }

        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }


    public function noticePolicyDel()
    {
        try {
            $id = request('id');
            $noticePolicy = new NoticePolicy();
            $re = $noticePolicy->delNoticePolicy($id);
            if (!$re) return $this->ajaxFail();
            return $this->ajaxSuccess();

        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }




}